/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.asset.workItem.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.asset.workItem.entity.WorkItemList;
import com.fuyao.cloud.asset.workItem.service.WorkItemListService;
import com.fuyao.cloud.common.biz.dto.PageQuery;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.asset.workItem.entity.WorkItem;
import com.fuyao.cloud.asset.workItem.service.WorkItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 工作事项表
 *
 * @author lyhills
 * @date 2023-09-11 12:02:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/workitem" )
@Api(value = "workitem", tags = "工作事项表管理")
public class WorkItemController {

    private final  WorkItemService workItemService;
    private final WorkItemListService workItemListService;

    /**
     * 分页查询
     * @param pageQuery 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_view')" )
    public R getWorkItemPage(PageQuery<WorkItem> pageQuery) {
        return R.ok(workItemService.page(pageQuery.getPage(), Wrappers.query(pageQuery.getExample())));
    }


    /**
     * 通过id查询工作事项表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(workItemService.getById(id));
    }

    /**
     * 新增工作事项表
     * @param workItem 工作事项表
     * @return R
     */
    @ApiOperation(value = "新增工作事项表", notes = "新增工作事项表")
    @SysLog("新增工作事项表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('workItem_workitem_add')" )
    public R save(@RequestBody WorkItem workItem) {
        boolean save = workItemService.save(workItem);
        return R.ok(workItem);
    }

    /**
     * 修改工作事项表
     * @param workItem 工作事项表
     * @return R
     */
    @ApiOperation(value = "修改工作事项表", notes = "修改工作事项表")
    @SysLog("修改工作事项表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('workItem_workitem_edit')" )
    public R updateById(@RequestBody WorkItem workItem) {
        return R.ok(workItemService.updateById(workItem));
    }

    /**
     * 通过id删除工作事项表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除工作事项表", notes = "通过id删除工作事项表")
    @SysLog("通过id删除工作事项表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_del')" )
    public R removeById(@PathVariable Long id) {
        boolean b = workItemService.removeById(id);
        if(b){
            workItemListService.remove(Wrappers.<WorkItemList>query().lambda().eq(WorkItemList::getParentId,id));
        }
        return R.ok(b);
    }


    /**
     * 导出excel 表格
     * @param workItem 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('workItem_workitem_export')" )
    public List<WorkItem> export(WorkItem workItem) {
        return workItemService.list(Wrappers.query(workItem));
    }

    /**
    * 创建方法
    * @return
    */
    @GetMapping(value = "/create")
    public R create(Long formId) {
        WorkItem workItem = workItemService.create();
        workItem.setFormId(formId);
        return R.ok(workItem);
    }

    /**
    * 批量删除方法
    * @param ids
    * @return R
    */
    @ApiOperation(value = "批量删除", notes = "批量删除通知公告")
    @SysLog("批量删除工作事项表" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('workItem_workitem_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        boolean b = workItemService.removeByIds(ids);
        if(b){
            workItemListService.remove(Wrappers.<WorkItemList>query().lambda().in(WorkItemList::getParentId,ids));
        }
        return R.ok(workItemService.removeByIds(ids));
    }
}
